﻿using System;
using System.ComponentModel;
using System.Web.UI;
using DotNetNuke.UI.UserControls;
using System.Web;

namespace HeroldIT.Dnn.Modules.WebControls
{
    /// <summary>
    /// A control for making a <see cref="TextEditor"/> control usable through
    /// the <see cref="ITextControl"/> interface.
    /// </summary>
    public class TextEditorWrapper : Control, ITextControl
    {
        #region constants

        private const string EmptyValue = "<p>&#160;</p>";

        #endregion

        #region properties

        private string textEditorID = "";

        /// <summary>
        /// Gets or sets the id of the referenced <see cref="TextEditor"/> control.
        /// </summary>
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [IDReferenceProperty(typeof(TextEditor))]
        [Localizable(false)]
        [DefaultValue("")]
        public string TextEditorID
        {
            get
            {
                return this.textEditorID;
            }
            set
            {
                if (null == value)
                    value = "";
                this.textEditorID = value;
                if (null != this.text && null != this.TextEditor)
                {
                    this.TextEditor.Text = this.text;
                }
            }
        }

        /// <summary>
        /// Gets the referenced <see cref="TextEditor"/> control or null if currently unavailable.
        /// </summary>
        protected TextEditor TextEditor
        {
            get
            {
                if (String.IsNullOrEmpty(this.TextEditorID))
                    return null;

                return this.NamingContainer.FindControl(this.TextEditorID) as TextEditor;
            }
        }

        private string text;

        /// <summary>
        /// Gets or sets the referenced <see cref="TextEditor"/> control's Text.
        /// </summary>
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (null != this.TextEditor)
                {
                    this.text = this.TextEditor.Text;
                    if (this.TextEditor.HtmlEncode)
                    {
                        if (this.text.Equals(HttpUtility.HtmlEncode(EmptyValue), StringComparison.OrdinalIgnoreCase))
                        {
                            this.text = "";
                        }
                    }
                    else
                    {
                        if (this.text.Equals(EmptyValue, StringComparison.OrdinalIgnoreCase))
                        {
                            this.text = "";
                        }
                    }
                }
                return this.text;
            }
            set
            {
                this.text = value;
                if (null != value && null != this.TextEditor)
                {
                    this.TextEditor.Text = String.IsNullOrEmpty(value)
                                               ? (this.TextEditor.HtmlEncode
                                                      ? HttpUtility.HtmlEncode(EmptyValue)
                                                      : EmptyValue)
                                               : value;
                }
            }
        }

        #endregion

        #region page lifecycle

        /// <summary>
        /// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
        }

        #endregion
    }
}
